package com.shy.collect.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shy.collect.entity.DicPayType;
import com.shy.collect.entity.DicPayWay;
import com.shy.collect.entity.PayProduct;
import com.shy.collect.entity.PayWay;
import com.shy.collect.mapper.DicPayTypeMapper;
import com.shy.collect.mapper.DicPayWayMapper;
import com.shy.collect.mapper.PayProductMapper;
import com.shy.collect.mapper.PayWayMapper;
import com.shy.collect.service.PayWayService;
import com.shy.configs.common.enums.PublicStatus;
import com.shy.configs.common.enums.RCode;
import com.shy.configs.common.exceptions.DftException;
import com.shy.configs.common.utils.IdUtil;
import com.shy.configs.mvc.configuration.BossInitConfig;
import com.shy.configs.mvc.models.query.PayWayQuery;
import com.shy.configs.mvc.models.vo.DicItemVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PayWayServiceImpl implements PayWayService {
    @Resource
    private PayWayMapper payWayMapper;
    @Resource
    private PayProductMapper payProductMapper;
    @Resource
    private DicPayWayMapper dicPayWayMapper;
    @Resource
    private DicPayTypeMapper dicPayTypeMapper;

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public IPage<PayWay> pageList(String productCode, Integer pageNo, Integer pageSize) {
        QueryWrapper<PayWay> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("pay_product_code", "way_code", "type_code");
        if (StrUtil.isNotBlank(productCode)) {
            queryWrapper.eq("pay_product_code", productCode);
        }
        return payWayMapper.selectPage(Page.of(pageNo, pageSize), queryWrapper);
    }

    @Override
    public List<DicItemVo> getPayWayDicList() {
        return BossInitConfig.PAY_WAY_DIC_LIST;
    }

    @Override
    public List<DicItemVo> getPayTypeDicList(String wayCode) {
        List<DicItemVo> dicItemVoList = BossInitConfig.PAY_TYPE_DIC_MAP.get(wayCode);
        return dicItemVoList == null ? new ArrayList<>() : dicItemVoList;
    }

    @Override
    public void createPayWay(PayWayQuery payWayQuery) {
        //查询产品编码是否存在
        PayProduct payProductCode = payProductMapper.selectOne(new QueryWrapper<PayProduct>().eq("product_code", payWayQuery.getProductCode()));
        if (payProductCode == null) {
            throw new DftException(RCode.PRODUCT_NONE);
        }
        //查询支付方式是否存在
        DicPayWay dicPayWay = dicPayWayMapper.selectOne(new QueryWrapper<DicPayWay>().eq("pay_way_code", payWayQuery.getWayCode()));
        if (dicPayWay == null) {
            throw new DftException(RCode.PAY_WAY_NONE);
        }
        //查询支付类型是否存在
        DicPayType dicPayType = dicPayTypeMapper.selectOne(new QueryWrapper<DicPayType>().eq("pay_way_code", payWayQuery.getWayCode()).eq("pay_type_code", payWayQuery.getTypeCode()));
        if (dicPayType == null) {
            throw new DftException(RCode.PAY_TYPE_NONE);
        }
        //业务逻辑 pay_way表 pay_way_code pay_type_code product_code三者是唯一的
        QueryWrapper<PayWay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("way_code", payWayQuery.getWayCode()).eq("type_code", payWayQuery.getTypeCode()).eq("pay_product_code", payWayQuery.getProductCode());
        PayWay payWay = payWayMapper.selectOne(queryWrapper);
        if (payWay != null) {
            throw new DftException(RCode.PAY_NONE);
        }
        payWay = new PayWay();
        payWay.setId(IdUtil.uuid());
        payWay.setWayCode(payWayQuery.getWayCode());
        payWay.setWayName(dicPayWay.getPayWayName());
        payWay.setTypeCode(payWayQuery.getTypeCode());
        payWay.setTypeName(dicPayType.getPayTypeName());
        payWay.setPayProductCode(payWayQuery.getProductCode());
        payWay.setPayRate(payWayQuery.getPayRate());
        payWay.setSorts(payWayQuery.getSorts());
        payWay.setStatus(PublicStatus.ENABLE.name());
        payWay.setEditTime(new Date());
        payWay.setRemark("创建支付配置");
        payWayMapper.insert(payWay);

    }

    @Override
    public void removeById(PayWayQuery payWayQuery) {
        QueryWrapper<PayWay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("way_code", payWayQuery.getWayCode())
                    .eq("type_code", payWayQuery.getTypeCode())
                    .eq("pay_product_code", payWayQuery.getProductCode());
        int delete = payWayMapper.delete(queryWrapper);
        if (delete == 0) {
            throw new DftException(RCode.FAIL);
        }
    }
}
